package com.example.friend.service.exam.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.example.common.core.constants.TTLConstants;
import com.example.common.core.domain.TableDataInfo;
import com.example.common.security.utils.ThreadLocalUtil;
import com.example.friend.domain.exam.dto.ExamQueryDTO;
import com.example.friend.domain.exam.dto.ExamRankDTO;
import com.example.friend.domain.exam.vo.ExamRankVO;
import com.example.friend.domain.exam.vo.ExamVO;
import com.example.friend.domain.user.vo.UserVO;
import com.example.friend.manger.UserCacheManager;
import com.example.friend.mapper.exam.ExamMapper;
import com.example.friend.mapper.user.UserExamMapper;
import com.example.friend.service.exam.IExamService;
import com.example.friend.manger.ExamCacheManager;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class ExamServiceImpl implements IExamService {
    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private ExamCacheManager examCacheManager;
    @Autowired
    private UserExamMapper userExamMapper;
    @Autowired
    private UserCacheManager userCacheManager;
    //获取竞赛列表，mysql法
    @Override
    public List<ExamVO> list(ExamQueryDTO examQueryDTO) {

        //设置自动分页查询
        PageHelper.startPage(examQueryDTO.getPageNum(), examQueryDTO.getPageSize());
        //从数据库中查询题目列表
        List<ExamVO> examVOList = examMapper.selectExamList(examQueryDTO);
        return examVOList;
    }

    //获取竞赛列表，redis缓存法
    public TableDataInfo reidsList(ExamQueryDTO examQueryDTO) {
        //从redis中获取 竞赛列表数据
        Long total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        List<ExamVO> examVOList;
        if (total == null || total == 0) {
            //如果大小为0，说明缓存中没有数据，需要先
            //刷新缓存
            examCacheManager.refreshCache(examQueryDTO.getType(),null);
        }else{
            //如果不为0，则直接redis中查询数据
        }
        examVOList = examCacheManager.getExamVOList(examQueryDTO,null);
        total = examCacheManager.getListSize(examQueryDTO.getType(),null);
        if (CollectionUtil.isEmpty(examVOList)) {
            return TableDataInfo.empty();
        }
        //是否报名
        assembleExamVOList(examVOList);
        return TableDataInfo.success(examVOList,total);
    }

    //获取竞赛的第一道题目
    @Override
    public String getFirstQuestion(Long examId) {
        //刷新redis缓存
        checkAndRefresh(examId);
        return examCacheManager.getFirstQuestion(examId).toString();
    }

    //获取竞赛上一题

    @Override
    public String preQuestion(Long examId, Long questionId) {
        checkAndRefresh(examId);
        return examCacheManager.preQuestion(examId, questionId).toString();
    }
    //获取竞赛下一题
    @Override
    public String nextQuestion(Long examId, Long questionId) {
        checkAndRefresh(examId);
        return examCacheManager.nextQuestion(examId, questionId).toString();
    }

    @Override
    public TableDataInfo rankList(ExamRankDTO examRankDTO) {
        Long total = examCacheManager.getRankListSize(examRankDTO.getExamId());
        List<ExamRankVO> examRankVOList;
        if (total == null || total <= 0) {
            PageHelper.startPage(examRankDTO.getPageNum(), examRankDTO.getPageSize());
            examRankVOList = userExamMapper.selectExamRankList(examRankDTO.getExamId());
            examCacheManager.refreshExamRankCache(examRankDTO.getExamId());
            total = new PageInfo<>(examRankVOList).getTotal();
        } else {
            examRankVOList = examCacheManager.getExamRankList(examRankDTO);
        }
        if (CollectionUtil.isEmpty(examRankVOList)) {
            return TableDataInfo.empty();
        }
        assembleExamRankVOList(examRankVOList);
        return TableDataInfo.success(examRankVOList, total);
    }

    private void checkAndRefresh(Long examId) {
        Long listSize = examCacheManager.getExamQuestionListSize(examId);
        if (listSize == null || listSize <= 0) {
            examCacheManager.refreshExamQuestionCache(examId);
        }
    }
    private void assembleExamRankVOList(List<ExamRankVO> examRankVOList) {
        if (CollectionUtil.isEmpty(examRankVOList)) {
            return;
        }
        for (ExamRankVO examRankVO : examRankVOList) {
            Long userId = examRankVO.getUserId();
            UserVO user = userCacheManager.getUserById(userId);
            examRankVO.setNickName(user.getNickName());
        }
    }
    private void assembleExamVOList(List<ExamVO> examVOList) {
        Long userId = ThreadLocalUtil.get(TTLConstants.USER_ID, Long.class);
        List<Long> userExamIdList = examCacheManager.getAllUserExamList(userId);
        if (CollectionUtil.isEmpty(userExamIdList)) {
            return;
        }
        for (ExamVO examVO : examVOList) {
            if (userExamIdList.contains(examVO.getExamId())) {
                examVO.setEnter(true);
            }
        }
    }
}
